import { actionTaskUser } from '@/api/flow/work'
import { AGREE_ACTION } from '@/utils/actions'

export default {

  computed: {
    tid () {
      return this.$route.params.tid
    },
  },

  data () {
    return {
      formFields: [this.field],
    }
  },

  props: {
    field: {
      type: Object,
      default: () => ({}),
    },
    workDetails: {
      type: Object,
      default: () => ({}),
    },
  },

  methods: {
    // action data
    actionData (formFields) {
      const { task_node } = this.workDetails
      const { id, name } = task_node

      return {
        task_node: id,
        // 被沟通的人
        communicate_user: undefined,
        // # 被转办的人
        forward_user: undefined,
        choice_child: [],
        // 表单名称
        work_name: name,
        // 表单数据
        fields_data: formFields ? this.getFieldsDatas(formFields) : [],
      }
    },

    // submitLoan
    handlerSubmit () {
      this.loading = true

      console.log(this.workDetails, 'dadada')

      this.form.validateFields((err, values) => {
        if (!err) {
          this.submitAction(AGREE_ACTION, this.actionData(this.formFields))
            .then(async ({ message }) => {
              this.$notification.success({ message })

              this.$emit('update')
            })
        }
      })
    },

    // Action 提交
    submitAction (action, actionData) {
      return new Promise((resolve, reject) => {
        actionTaskUser(this.tid, action, actionData)
          .then(async ({ status, message }) => {
            resolve({ status, message })
          })
          .catch((err) => {
            reject(err)
          })
          .finally(() => {})
      })
    },

    // 获取表单提交数据
    getFieldsDatas (formFields) {
      const fieldsValue = this.form.getFieldsValue()

      const datas = formFields
        .map(({ field: f, value, label, callback }) => {
          return {
            field: f.id,
            field_value: { value: callback(fieldsValue[label]) },
          }
        })

      return datas
    },
  },
}
